﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Einstein.Utility;
using Einstein.Data; 
using DevComponents.DotNetBar;
using Einstein.Group.DAL;

namespace Einstein.Platform.Window.Config
{
    public partial class Config_Role_New : Office2007Form
    {
        public Config_Role_New()
        {
            InitializeComponent();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (superValidator1.Validate())
            {
                RoleInfoDAL dal = new RoleInfoDAL();
                RoleInfo info = new RoleInfo { RoleName = txtRoleName.Text, Desc = txtDesc.Text, CreateTime = DateTime.Now, CreatePerson = Context.CurrentUserName, UpdatePerson = Context.CurrentUserName, UpdateTime = DateTime.Now, IsSys=false };
                bool isEdit = false;
                if (this.RoleInfo != null)
                {
                    info.RoleID = this.RoleInfo.RoleID;
                    isEdit = true;
                }
                if (dal.Exist(info))
                {
                    BoxManager.ShowWarning("已经存在相同的角色名称，请重试。");
                    return;
                }
                bool result = isEdit ? dal.Update(info) : dal.Add(info);
                if (result)
                {
                    BoxManager.ShowSuccess();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    BoxManager.ShowFail();
                }
            }
        }

        public RoleInfo RoleInfo
        {
            get;
            set;
        }

        public void BindData()
        {
            if (RoleInfo != null)
            {
                txtRoleName.Text = RoleInfo.RoleName;
                txtDesc.Text = RoleInfo.Desc;
            }
        }
    }
}
